package com.zzyl.serve.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzyl.serve.vo.ElderPageVo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.zzyl.serve.domain.FamilyMember;

/**
 * 老人家属Mapper接口
 * 
 * @author itheima
 * @date 2025-03-12
 */
@Mapper
public interface FamilyMemberMapper extends BaseMapper<FamilyMember>
{
    /**
     * 查询老人家属
     * 
     * @param id 老人家属主键
     * @return 老人家属
     */
    public FamilyMember selectFamilyMemberById(Long id);

    /**
     * 查询老人家属列表
     * 
     * @param familyMember 老人家属
     * @return 老人家属集合
     */
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember);

    /**
     * 新增老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    public int insertFamilyMember(FamilyMember familyMember);

    /**
     * 修改老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    public int updateFamilyMember(FamilyMember familyMember);

    /**
     * 删除老人家属
     * 
     * @param id 老人家属主键
     * @return 结果
     */
    public int deleteFamilyMemberById(Long id);

    /**
     * 批量删除老人家属
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFamilyMemberByIds(Long[] ids);

    //h
    @Insert("insert into family_member_elder(family_member_id,elder_id,create_time,remark) values(#{familyMemberId},#{elderId},#{createTime},#{remark})")
    void BindingFamilyMember(@Param("familyMemberId")Long familyMemberId,
                             @Param("elderId") Long elderId,
                             @Param("createTime") LocalDateTime createTime,
                             @Param("remark") String remark);

    @Select("select * from family_member_elder where family_member_id=#{userId} and elder_id=#{elderId}")
    List<Map<String, Object>> find(@Param("userId") Long userId, @Param("elderId") Long elderId);


    @Select("select f.id , f.family_member_id familyMemberId ,f.elder_id elderId,e.name elderName \n" +
            "from  family_member_elder f join zzyl.elder e on e.id = f.elder_id")
    List<Map<String, Object>>  myFamilyMember();


    @Select("select fme.id as mid ,fme.remark as mremark , fme.elder_id , e.name , e.image , b.bed_number, r.type_name ,  d.iot_id , d.device_name , d.product_key\n" +
            "from family_member_elder fme\n" +
            "    left join elder e on fme.elder_id = e.id\n" +
            "left join bed b on e.bed_id = b.id\n" +
            "left join device d on e.id = d.binding_location\n" +
            "left join room r on b.room_id = r.id \n" +
            "where fme.family_member_id = #{userId}")
    List<ElderPageVo> PageFamilyMember(@Param("userId") Long userId);
    @Delete("delete from family_member_elder where id=#{id}")
    void deleteByIdFamilyMember(@Param("id") Integer id);
}

